package com.itmate.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.itmate.entity.Users;
import com.itmate.service.UsersService;
import com.lj.dao.BorrowBooksmpl;

/**
 * Servlet implementation class DoLogin
 */
@WebServlet("/dologin")
public class DoLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoLogin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doPost(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
		
		String error="";
		String username =request.getParameter("username");
		String password =request.getParameter("password");
		Users mu=new Users();
		mu.setUser_name(username);
		mu.setPassword(password);
		BorrowBooksmpl bbs = new BorrowBooksmpl();
		Users mu2 = bbs.findUser(username, password);
		UsersService mus =new UsersService();
		if(mus.login(mu)) {
			HttpSession session = request.getSession();
			session.setAttribute("username", username);
			System.out.println("登录成功");
			request.setAttribute("username",mu.getUser_name());
			session.setAttribute("user_id",mu2.getUser_id());
			request.getRequestDispatcher("/WEB-INF/jsp/Homepage.jsp").forward(request, response);
		}else {
			System.out.print(username+"密码错误");
			error="密码错误";
			request.setAttribute("error",error);
			request.getRequestDispatcher("/WEB-INF/jsp/Error.jsp").forward(request, response);
		}
	}

}
